package org.rqb.frame;

import com.github.kwhat.jnativehook.GlobalScreen;
import com.github.kwhat.jnativehook.mouse.NativeMouseEvent;
import com.github.kwhat.jnativehook.mouse.NativeMouseListener;
import com.github.kwhat.jnativehook.mouse.NativeMouseMotionListener;
import com.sun.jna.platform.win32.WinDef;
import lombok.extern.slf4j.Slf4j;
import org.rqb.listener.GlobalMouseListener;
import org.rqb.panel.GesturePanel;
import org.rqb.panel.partPanel.RepeatNumPanel;

import javax.swing.*;
import java.awt.*;

@Slf4j
public class GestureWindow implements NativeMouseListener, NativeMouseMotionListener {
    private static GestureWindow instance;

    private GesturePanel gesturePanel;
    private JWindow gestureWin;
    private WinDef.HWND hwnd;
    private RepeatNumPanel repeatNumPanel;
    private GlobalMouseListener mouseGesturesGlobalMouseListener;

    public static GestureWindow getInstance() {
        if (instance == null) {
            instance = new GestureWindow();
        }
        return instance;
    }

    private GestureWindow() {
        //透明窗口画布初始化
        componentInit();
    }

    /**
     * 鼠标手势画笔监听器开启
     */
    public void startMouseGestures() {
        // 4. 注册钩子并添加监听器
        GlobalScreen.addNativeMouseListener(this);
        GlobalScreen.addNativeMouseMotionListener(this);

        //开启四个角全局监听器
        if (mouseGesturesGlobalMouseListener == null) {
            mouseGesturesGlobalMouseListener = new GlobalMouseListener();
        }
        GlobalScreen.addNativeMouseMotionListener(mouseGesturesGlobalMouseListener);
    }
    /**
     * 鼠标手势画笔监听器关闭
     */
    public void stopMouseGestures() {
        GlobalScreen.removeNativeMouseListener(this);
        GlobalScreen.removeNativeMouseMotionListener(this);
        if (mouseGesturesGlobalMouseListener != null) {
            GlobalScreen.removeNativeMouseMotionListener(mouseGesturesGlobalMouseListener);
        }
    }

    /**
     * 组件初始化
     */
    private void componentInit() {
        //设置窗口
        gestureWin = new JWindow();
        gestureWin.setName("MyOverlay");
        gestureWin.setAlwaysOnTop(true);
        gestureWin.setBackground(new Color(0,0,0,0));
        gestureWin.setBounds(GraphicsEnvironment
                .getLocalGraphicsEnvironment()
                .getMaximumWindowBounds());

        // 使用 null 布局，这样可以精确控制组件位置
        gestureWin.setLayout(null);

        //设置画布
        gesturePanel = new GesturePanel();
        gesturePanel.setOpaque(false);
        gesturePanel.setBounds(gestureWin.getBounds()); // 设置画布覆盖整个窗口


        gestureWin.add(gesturePanel);

        // 3. 默认 透传鼠标事件
        gestureWin.setVisible(true);
    }

    /**
     * 开启显示已经复现次数
     * @param repeatNum
     */
    public RepeatNumPanel startRepeatNumPanel(String repeatNum) {
        if (repeatNumPanel == null) {
            repeatNumPanel = new RepeatNumPanel(repeatNum);
            // 设置面板大小
            repeatNumPanel.setSize(100, 30); // 调整为合适的大小

            // 计算屏幕顶部居中位置
            Rectangle screenBounds = GraphicsEnvironment
                    .getLocalGraphicsEnvironment()
                    .getMaximumWindowBounds();
            int x = (screenBounds.width - repeatNumPanel.getWidth()) / 2;
            int y = 10; // 距离屏幕顶部10像素

            repeatNumPanel.setLocation(x, y);

        }
        repeatNumPanel.setRepeatNum(repeatNum);
        gestureWin.add(repeatNumPanel);
        gestureWin.repaint();
        return repeatNumPanel;
    }

    /**
     * 关闭已经复现次数
     */
    public RepeatNumPanel stopRepeatNumPanel() {
        if (repeatNumPanel != null) {
            gestureWin.remove(repeatNumPanel);
            gestureWin.repaint();
        }
        return repeatNumPanel;
    }



    @Override
    public void nativeMousePressed(NativeMouseEvent nativeEvent) {
        if (nativeEvent.getButton() == NativeMouseEvent.BUTTON2) {
        }
    }

    @Override
    public void nativeMouseReleased(NativeMouseEvent nativeEvent) {
        if (nativeEvent.getButton() == NativeMouseEvent.BUTTON2) {//表示是鼠标右键
            gesturePanel.clear(); //清空鼠标轨迹
            gesturePanel.repaint();
        }
    }

    @Override
    public void nativeMouseMoved(NativeMouseEvent nativeEvent) {
    }

    @Override
    public void nativeMouseDragged(NativeMouseEvent nativeEvent) {
        // 调试输出，确认事件到达
        if ((nativeEvent.getModifiers() & NativeMouseEvent.BUTTON2_MASK) != 0) { //表示是鼠标右键被按下
            log.info("Dragged: " + nativeEvent.getX() + "," + nativeEvent.getY());

            gesturePanel.addPoint(nativeEvent.getPoint());
            gestureWin.repaint();
        }
    }
}
